/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.ui.list;

import com.yy.bugzillahelp.MainFrame;
import com.yy.bugzillahelp.data.BugDetailData;
import com.yy.bugzillahelp.data.server.ServerData;
import com.yy.bugzillahelp.ui.detail.BugDetailPanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Alan
 */
public class BugListTable extends JTable {

    private ServerData connectData;
    private BugListTableModel bugListTableModel;

    public BugListTable(ServerData connectData) {
        this.connectData = connectData;
        bugListTableModel = new BugListTableModel(ColumnNames.getColumnNames().toArray(new String[]{}), 0);
        this.setModel(bugListTableModel);

        this.setRowHeight(20);
        this.setAutoCreateRowSorter(true);

        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

                if (e.getClickCount() == 2) {
                    showBugDetail(e);
                }
            }
        });
    }

    public void setDatas(ArrayList<BugDetailData> datas) {
        bugListTableModel.setDatas(datas);
    }

    private void showBugDetail(MouseEvent e) {
        int row = this.rowAtPoint(e.getPoint());
        if (row >= 0) {
            BugDetailPanel bugDetailPanel = new BugDetailPanel(connectData);
            String id = this.getValueAt(row, 0).toString();
            bugDetailPanel.show(id);
            MainFrame.getFrame().addTab("Bug详情-" + id, bugDetailPanel);
        }
    }

    private class BugListTableModel extends DefaultTableModel {

        private ArrayList<BugDetailData> datas = new ArrayList<BugDetailData>();

        public BugListTableModel(String[] columnNames, int rowCount) {
            super(columnNames, rowCount);
        }

        @Override
        public int getRowCount() {
            if (datas == null) {
                return 0;
            } else {
                return datas.size();
            }
        }

        public void setDatas(ArrayList<BugDetailData> datas) {
            this.datas = datas;
            fireTableDataChanged();
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            String columnName = getColumnName(columnIndex);
            if (ColumnNames.ID.equals(columnName)) {
                return Integer.class;
            } else {
                return String.class;
            }
        }

        @Override
        public Object getValueAt(int row, int column) {
            if (row >= 0 && row < getRowCount()) {
                BugDetailData data = datas.get(row);
                String columnName = getColumnName(column);
                if (ColumnNames.ID.equals(columnName)) {
                    return Integer.parseInt(data.getBugId());
                } else if (ColumnNames.ID.equals(columnName)) {
                    return data.getBugId();
                } else if (ColumnNames.TITLE.equals(columnName)) {
                    return data.getShortDesc();
                } else if (ColumnNames.PRODUCT.equals(columnName)) {
                    return data.getProduct();
                } else if (ColumnNames.COMPONENT.equals(columnName)) {
                    return data.getComponent();
                } else if (ColumnNames.VERSION.equals(columnName)) {
                    return data.getVersion();
                } else if (ColumnNames.STATUS.equals(columnName)) {
                    return data.getStatus();
                } else if (ColumnNames.RESOLUTION.equals(columnName)) {
                    return data.getResolution();
                } else if (ColumnNames.PRIORITY.equals(columnName)) {
                    return data.getPriority();
                } else if (ColumnNames.SEVERITY.equals(columnName)) {
                    return data.getSeverity();
                } else if (ColumnNames.REPORTER.equals(columnName)) {
                    return data.getReporter();
                } else if (ColumnNames.ASSIGNED_TO.equals(columnName)) {
                    return data.getAssignedTo();
                } else {
                    return "";
                }
            } else {
                return "";
            }
        }

        @Override
        public void setValueAt(Object aValue, int row, int column) {
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    }
}
